<!-- 设备保养-报告-保养卡片 -->
<template>
  <div style="width: 100%; height: 100%; margin: 0">
    <iframe id="PMS_IFRAME_REPORT" width="100%" height="100%" name="ALL_EQUIP_EPM_REPORT" frameborder="0" :src="url" @load="onLoad" />
  </div>
</template>

<script setup lang="ts">
import { getToken } from "@/utils/auth";
import { onMounted, ref } from "vue";
import { useRouter } from "vue-router";

const router = useRouter();

// defineOptions({
//   name: "ALL_EQUIP_EPM_REPORT"
// });
const accessToken = getToken().accessToken || "";

const isShow = ref(true);

const permission = localStorage.getItem("pmsPermission");

const onLoad = () => {
  const iframeReport = document.getElementById("PMS_IFRAME_REPORT") as HTMLIFrameElement;
  iframeReport?.contentWindow?.postMessage(JSON.parse(permission), "*");
};

const facCode = localStorage.getItem("facCode");

const url = `${import.meta.env.VITE_API_URL_PMS_NEW}/#/report/abnormal?ssoToken=${accessToken}&isHiddenMenu=true&facCode=${facCode}`;

onMounted(() => {
  isShow.value = true;
  window.addEventListener("message", event => {
    // 确保消息来源为pms
    if (event.origin !== import.meta.env.VITE_API_URL_PMS_NEW) return;
    const pmsCodes = ["TBMBtn", "CBMBtn", "UNSBtn", "PlanList", "PlanBtn"];
    if (pmsCodes.indexOf(JSON.parse(event.data)?.code) > -1) {
      isShow.value = false;
      setTimeout(() => {
        isShow.value = true;
      }, 0);
    } else if (JSON.parse(event.data)?.code === "JobList") {
      router.push({ path: "/all/equip/epm/task/list", query: { fullpath: JSON.parse(event.data).fullpath } });
    }
  });
});
</script>
